package com.haircut.api.project;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.haircut.project.service.record.IRecordService;
import com.haircut.project.vo.record.ApiRecordDetailVo;
import com.haircut.project.vo.record.ApiRecordReqVo;
import com.haircut.project.vo.record.ApiRecordResVo;
import com.haircut.project.vo.record.ApiRecordVo;
import com.haircut.sys.utils.user.SecurityUtils;
import com.modular.constant.NumberConstant;
import com.modular.result.PageResult;
import com.modular.result.RspMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "项目信息")
@RestController
@RequestMapping("/api/record")
public class ApiRecordController {
    @Autowired
    private IRecordService recordService;


    /**
     * 查询分类信息

     * @return
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/saveRecord")
    public RspMessage<Void> saveRecord(@Validated @RequestBody ApiRecordVo recordVo){
        recordService.saveRecord(recordVo);
        return RspMessage.success();
    }

    /**
     * 查询分类信息

     * @return
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/moidfyRecord")
    public RspMessage<Void> moidfyRecord(@Validated @RequestBody ApiRecordVo recordVo){
        recordService.moidfyRecord(recordVo);
        return RspMessage.success();
    }

    /**
     * 分页查询我的记录信息
     * @return
     */
    @ApiOperation(value = "查询我的记录信息")
    @PostMapping("/findPage")
    public RspMessage<PageResult<ApiRecordResVo>> findPage(@Validated @RequestBody ApiRecordReqVo reqVo){

        reqVo.setUserId(SecurityUtils.getUserId());

        if(ObjectUtil.equal(reqVo.getTimeType(), NumberConstant.ONE)){
            Date currDate = DateUtil.date();
            reqVo.setStatTime(DateUtil.beginOfDay(currDate));
            reqVo.setEndTime(DateUtil.endOfDay(currDate));
        }else{
            Date currDate = DateUtil.date();
            reqVo.setStatTime(DateUtil.beginOfDay(DateUtil.offsetMonth(currDate,-3)));
            reqVo.setEndTime(DateUtil.endOfDay(currDate));
        }


        return RspMessage.success(recordService.findPage(reqVo));
    }

    /**
     * 查询记录详情
     * @return
     */
    @ApiOperation(value = "查询记录详情")
    @GetMapping("/findDetail")
    public RspMessage<ApiRecordDetailVo> findDetail(@ApiParam("id") @RequestParam("id") String id){

        return RspMessage.success(recordService.findDetail(id));
    }

}
